/**
 * @author Eric Jahn
 * Course: Object Oriented Programming
 * Final Project
 * Due Date: August 3, 2012
 */
package oop.chat.user;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * this class manages a contact list of a user
 * @author Eric Jahn
 */
public class ContactList implements Serializable 
{
    //hashmap for the contacts
	protected HashMap<String, Contact> userContacts;

    /**
     * default constructor, initializes the hashmap
     */
    public ContactList() 
	{
        this.userContacts = new HashMap<>();
    }

    /**
     * initializes the hashmap and adds a new contact
     * @param	contact	Contact
     */
    public ContactList(Contact contact) 
	{
        this.userContacts = new HashMap<>();
        this.userContacts.put(contact.getPID(), contact);
    }

    /**
     * gets the contact list
     * @return	contactList	HashMap<Contact, Contact>
     */
    public final HashMap<String, Contact> getUserContacts() 
	{
        return this.userContacts;
    }

    /**
     * adds a new contact to the list
     * @param	contact		Contact
     */
    public void addContact(Contact contact) 
	{
        this.userContacts.put(contact.getPID(), contact);
    }

	/**
	 * gets a contact
	 * @param	PID			String
	 * @return	Contact
	 */
    public Contact getContact(String PID)
    {
        return this.userContacts.get(PID);
    }
    /**
     * removes the contact from the contact list
     * @param	contact		Contact
     */
    public void removeContact(Contact contact) 
	{
        this.userContacts.remove(contact.getPID());
    }

    /**
     * removes all contacts from the contact list
     */
    public void clearContactList() 
	{
        this.userContacts.clear();
    }
	
	/**
	 * check if the contact list is empty
	 * @return	boolean
	 */
	public final boolean isListEmpty()
	{
		return this.userContacts.isEmpty();
	}

    public void makeFavorite(Contact contact) 
	{
    }
    
    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer("ContactList: ");
        Set entries = userContacts.entrySet();
        Iterator iter = entries.iterator();
        while (iter.hasNext())
        {
            Map.Entry entry = (Map.Entry) iter.next();
            buffer.append(entry.getKey());
            buffer.append(" ");
            buffer.append(entry.getValue());
            buffer.append("; ");
        }
        return buffer.toString();
    }
}